package com.pocketmenu.persistence;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class PreferenceDataSource {
	
	// Database fields
	  private SQLiteDatabase database;
	  private SQLiteHelper dbHelper;
	  private String[] allColumns = 
		  { SQLiteHelper.COLUMN_ID,
			SQLiteHelper.COLUMN_PREFERENCE_ID_INGREDIENT,
			SQLiteHelper.COLUMN_PREFERENCE_INCLUDE};

	  public PreferenceDataSource(Context context) {
	    dbHelper = new SQLiteHelper(context);
	  }

	  public void open() throws SQLException {
	    database = dbHelper.getWritableDatabase();
	  }

	  public void close() {
	    dbHelper.close();
	  }

	  public Preference create(int idIngredient, boolean include) {
	    ContentValues values = new ContentValues();
	    values.put(SQLiteHelper.COLUMN_PREFERENCE_ID_INGREDIENT, idIngredient);
	    values.put(SQLiteHelper.COLUMN_PREFERENCE_INCLUDE, include);	    
	    
	    long insertId = database.insert(SQLiteHelper.TABLE_PREFERENCE, null,
	        values);
	    
	    Cursor cursor = database.query(SQLiteHelper.TABLE_PREFERENCE,
	        allColumns, SQLiteHelper.COLUMN_ID + " = " + insertId, null,
	        null, null, null);
	    cursor.moveToFirst();
	    Preference newTemp = cursorToObject(cursor);
	    cursor.close();
	    return newTemp;
	  }

	  public void delete(Preference toDelete) {
	    int id = toDelete.getId();
	    System.out.println("Preference deleted with id: " + id);
	    database.delete(SQLiteHelper.TABLE_PREFERENCE, SQLiteHelper.COLUMN_ID
	        + " = " + id, null);
	  }
	  
	  public void deleteAll() {
		    
		    
		    database.delete(SQLiteHelper.TABLE_PREFERENCE, null,null);
		  }


	  public List<Preference> getAll() {
	    List<Preference> listTemp = new ArrayList<Preference>();

	    Cursor cursor = database.query(SQLiteHelper.TABLE_PREFERENCE,
	        allColumns, null, null, null, null, null);
	    Log.i("system.out", "aqui es: "+cursor.getCount());
	    cursor.moveToFirst();
	    while (!cursor.isAfterLast()) {
	      Preference temp = cursorToObject(cursor);
	      listTemp.add(temp);
	      cursor.moveToNext();
	    }
	    // Make sure to close the cursor
	    cursor.close();
	    return listTemp;
	  }
	  
	  public Cursor getNamesAndValid()
	  {
		  String query = "select oi." + SQLiteHelper.COLUMN_PREFERENCE_INCLUDE 
				  + ", i." + SQLiteHelper.COLUMN_INGREDIENT_NAME
				  + " from " + SQLiteHelper.TABLE_PREFERENCE
				  + " oi join " + SQLiteHelper.TABLE_INGREDIENT + " i on oi."+SQLiteHelper.COLUMN_PREFERENCE_ID_INGREDIENT +" = i." 
				  + SQLiteHelper.COLUMN_ID;		  		  
		  Cursor cursor = database.rawQuery(query, null);
		  return cursor;
	  }

	  private Preference cursorToObject(Cursor cursor) {
	    Preference temp = new Preference();
	    temp.setId(cursor.getInt(0));
	    temp.setIdIngredient(cursor.getInt(1));
	    
	    boolean include = cursor.getInt(2) != 0; 
	    temp.setInclude(include);
	    
	    return temp;
	  }
}
